package com.bw.lib_common.uiLoader

import android.content.Context
import android.os.Handler
import android.os.Looper
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.FrameLayout
import com.bw.lib_common.R
import com.bw.lib_common.databinding.ViewEmptyNoFollowBinding
import com.bw.lib_common.databinding.ViewEmptyUnLoginBinding
import com.scwang.smart.refresh.layout.SmartRefreshLayout

/**
 * @author ytx
 * @date 2025-09-25 13:53
 * @description
 */
 abstract class UILoader:FrameLayout{
    constructor(context: Context):this(context,null)
    constructor(context: Context,attributeSet: AttributeSet?):this(context,attributeSet,0)
    constructor(context: Context,attributeSet: AttributeSet?,defStyleAttr: Int):super(context,attributeSet,defStyleAttr)

    private var mLoadingView: View? = null
    private var mErrorView: View? = null
    private var mEmptyView: View? = null
    private var mSuccessView: View? = null
    private var mUnLoginView: View? = null
    private var mNoFollowView: View? = null

    var mCurrentState = UIState.NONE //当前状态
    var onLoginClick: (() -> Unit)? = null //登录回调
    protected var mIRetryClick: (()->Unit)? = null //重试回调


    private var isRefresh = false //是否刷新状态
    init {
        switchUIByState()
    }
    /**
     * 更新UI状态
     */
    fun updateState(state:UIState){
        if (mCurrentState != state){
            mCurrentState = state
            Handler(Looper.getMainLooper()).post{
                switchUIByState()//更新UI，主线程中
            }
        }
    }
    /**
     * 根据状态更新UI
     */
    private fun switchUIByState(){
        //成功
        if (mSuccessView == null){
            mSuccessView = loadSuccessView(this)
            addView(mSuccessView)
        }
        if (mLoadingView == null){
            mLoadingView = loadLoadingView()
            addView(mLoadingView)
        }

        if (mEmptyView == null) {
            mEmptyView = loadEmptyView()
            addView(mEmptyView)
        }
        if (mErrorView == null) {
            mErrorView = loadErrorView()
            addView(mErrorView)
        }
        if (mUnLoginView == null) {
            mUnLoginView = loadUnLoginView()
            addView(mUnLoginView)
        }
        if (mNoFollowView == null) {
            mNoFollowView = loadNoFollowView()
            addView(mNoFollowView)
        }

        //控制显示和隐藏
        mSuccessView?.visibility = if (mCurrentState == UIState.SUCCESS) VISIBLE else GONE
        mLoadingView?.visibility = if (mCurrentState == UIState.LOADING) VISIBLE else GONE
        mErrorView?.visibility = if (mCurrentState == UIState.NETWORK_ERROR) VISIBLE else GONE
        mEmptyView?.visibility = if (mCurrentState == UIState.EMPTY) VISIBLE else GONE
        mUnLoginView?.visibility = if (mCurrentState == UIState.UN_LOGIN) VISIBLE else GONE
        mNoFollowView?.visibility = if (mCurrentState == UIState.NO_FOLLOW) VISIBLE else GONE

        //背景
        this.setBackgroundResource(if (mCurrentState == UIState.SUCCESS) R.color.transparent else R.color.color_f8f8f8)

    }

    /**
     * 加载成功布局文件，该方法子类必须实现
     *
     * @param container 父容器
     * @return
     */
    protected abstract fun loadSuccessView(container: ViewGroup?): View?

    /**
     * 加载中UI
     */
    protected fun loadLoadingView(): View {
        return LayoutInflater.from(context).inflate(R.layout.view_loading, this, false)
    }

    /**
     * 未登录UI
     */
    private fun loadUnLoginView(): View {
        val binding = ViewEmptyUnLoginBinding.inflate(LayoutInflater.from(context))
        binding.btnGoLogin.setOnClickListener {
            onLoginClick?.invoke()
        }
        return binding.root
    }

    /**
     * 没有关注UI
     */

    private fun loadNoFollowView(): View {
        val binding = ViewEmptyNoFollowBinding.inflate(LayoutInflater.from(context))
        binding.btnGoFollow.setOnClickListener {

        }
        return binding.root
    }

    /**
     * 空数据UI
     */
    open fun loadEmptyView(): View {
        val emptyView = LayoutInflater.from(context).inflate(R.layout.view_empty_ui_with_refresh, this, false)
        emptyView.findViewById<SmartRefreshLayout>(R.id.l_refresh).setOnRefreshListener {
            if (mIRetryClick != null) {
                isRefresh = true
                mCurrentState = UIState.REFRESH
                mIRetryClick?.invoke()
            }
        }
        return emptyView
    }

    /**
     * 失败UI
     */
    protected fun loadErrorView(): View {
        //加载错误布局文件
        val errorView = LayoutInflater.from(context).inflate(R.layout.view_error_ui_with_refresh, this, false)
        //找到重试按钮，为其设置点击事件
        errorView.findViewById<SmartRefreshLayout>(R.id.l_refresh).setOnRefreshListener {
            if (mIRetryClick != null) {
                isRefresh = true
                mCurrentState = UIState.REFRESH
                mIRetryClick?.invoke()
            }
        }

        errorView.findViewById<Button>(R.id.btn_retry).setOnClickListener {
            if (mIRetryClick != null) {
                mIRetryClick?.invoke()
            }
        }
        return errorView
    }






}